package com.huijinhui.wlmtsys.controller;



import com.huijinhui.wlmtsys.constant.JwtClaimsConstant;
import com.huijinhui.wlmtsys.pojo.dtos.LikesBehaviorDto;
import com.huijinhui.wlmtsys.pojo.dtos.LoginFormDTO;
import com.huijinhui.wlmtsys.pojo.dtos.ResponseResult;
import com.huijinhui.wlmtsys.pojo.dtos.WxUserDTO;
import com.huijinhui.wlmtsys.pojo.entity.User;
import com.huijinhui.wlmtsys.pojo.vo.UserLoginVO;
import com.huijinhui.wlmtsys.properties.JwtProperties;
import com.huijinhui.wlmtsys.service.UserService;

import com.huijinhui.wlmtsys.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "用户相关接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    @Operation(summary = "用户登录接口")
    @PostMapping("login")
    public ResponseResult login(@RequestBody @Validated LoginFormDTO loginFormDTO){
        log.info("微信用户登录：{}",loginFormDTO.getCode());

        //微信登录
        User user = userService.wxLogin(loginFormDTO);

        //为微信用户生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .build();
        return ResponseResult.okResult(userLoginVO);
    }



}

